<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    /**
     * @param {number[]} nums
     * @return {number}
     */
    // var maximumProduct = function (nums) {
    //   nums.sort((a,b)=>a-b)
    //   return Math.max(nums[nums.length-1]*nums[nums.length-2]*nums[nums.length-3],nums[0]*nums[1]*nums[nums.length-1])
    // };

    var maximumProduct = function (nums) {
      let max1 = max2 = max3 = -Infinity;
      let min1 = min2 =Infinity;

      for (let i = 0; i < nums.length; i++) {
        let num = nums[i];
        if(num>max1){
          max3 = max2;
          max2 = max1;
          max1 = num;
        }else if(num>max2){
          max3 = max2;
          max2 = num;
        }else if(num>max3){
          max3 = num;
        }

        if(num<min1){
          min2 = min1;
          min1 = num;
        }else if(num<min2){
          min2 = num
        }
      }
      return Math.max(max1*max2*max3,min1*min2*max1)
    };

    console.log(maximumProduct([-100, -98, -1, 2, 3, 4]));
  </script>
</body>

</html>